package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.Member;
import com.itheima.POJO.MemberInfo;
import com.itheima.POJO.Order;
import com.itheima.POJO.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.Map;

/**
 * @date:
 * @author: majiao
 * @description:
 */
public interface PhysicalUploadMapper {
    // 体检上传:初始化查询所有
    @Select("<script> select * from t_member\n" +
            "     <where>\n" +
            "        <if test=\"value !=null and value.length > 0\">\n" +
            "          fileNumber like concat('%',#{value},'%') or name like concat('%',#{value},'%') or phoneNumber like concat('%',#{value},'%')\n" +
            "          </if>\n" +
            "     </where></script>")
    Page<Member> findPage(String queryString);


    // 体检上传:根据id删除会员信息
    @Delete("delete from t_memberInfo where id = #{value}")
    void deleteMemberInfo(Integer id);

    //插入会员表中并返回主键
    @Insert("INSERT INTO t_member(name,fileNumber,reportStatus) VALUES(#{name},#{fileNumber},#{reportStatus})")
    @Options(useGeneratedKeys = true,keyColumn = "id")
    void addMember(Member member);

    //更新会员信息表中的信息
    @Update("  <script>\n" +
            "    update t_memberinfo\n" +
            "    <set>\n" +
            "    <if test =\"height!=null\">\n" +
            "    height=#{height},\n" +
            "    </if>\n" +
            "     <if test =\"weight!=null\">\n" +
            "    weight=#{weight},\n" +
            "    </if>\n" +
            "     <if test =\"dbp!=null\">\n" +
            "    dbp=#{dbp},\n" +
            "    </if>\n" +
            "    <if test =\"sysp!=null\">\n" +
            "    sysp=#{sysp},\n" +
            "    </if>\n" +
            "    </set>\n" +
            "    where id = #{id}</script>")
    void updateMemberInfo(MemberInfo memberInfo);

    //新增按钮:点击文件上传后,插入数据库预约表中的会员id和体检时间
    @Insert("insert into t_order(member_id,orderDate) values(#{param1},#{param2})")
    void insertRelation(Integer memberId,String physicalTime);

    //根据会员id查询会员信息,回显页面
    @Select("select * from t_member where id = #{value}")
    Member findMemberById(Integer id);

    //根据会员id查询预约表的日期
    @Select("SELECT orderdate FROM t_order WHERE t_order.`member_id`=#{value}")
    Date findOrderDate(Integer id);


    //查询order表中的日期,但此时只有条件memberId,所以需要递进查询,先查询会员表所有根据id到order表查询日期
    @Select("select * from t_member where id = #{value}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "physicalTime",column = "id",
                    javaType = Date.class,
                    one = @One(select = "com.itheima.dao.PhysicalUploadMapper.findOrderDateByMemberId")),
    })
    Map<String,Object> findField(Integer memberId);

    // 上面方法.调用查询order中的日期
    @Select("SELECT orderDATE FROM t_order WHERE t_order.`member_id`=#{value}  ")
    @Results({
            @Result(property = "orderDate", column = "orderDate"),
    })
    Date findOrderDateByMemberId(Integer id);


    // 插入memberInfo表格数据,此时的memberInfo四个字段是从缓存中读取的,id是先插入表格返回的主键,组成对象
    @Insert("INSERT INTO t_memberinfo(id,height,weight,dbp,sysp) VALUES(#{id},#{height},#{weight},#{dbp},#{sysp})")
    void insertMemberInfo( MemberInfo memberInfo);

    // 点击上传后,修改状态为1
    @Update("update t_member set reportStatus='1' where id=#{id}")
    void updateMemberRepotStatus(Integer id);

    // 根据会员id查询在信息表中的次数
    @Select("select count(id) from t_memberinfo where id=#{id}")
    Long countMemberIdInfo(Integer id);

    // 根据会员id查询套餐id
    @Select("SELECT setmeal_id FROM t_order WHERE member_id =#{memberId} ")
    Integer findSetmealID(Integer memberId);

    // 根据套餐id查询套餐
    @Select("select name from t_setmeal where id = #{setmealID}")
    String findSetmealNameById(Integer setmealID);


    //删除后,修改报告状态为0
    @Update("update t_member set reportStatus='0' where id=#{id}")
    void updateStatus0(Integer id);

    // 查询报告状态,"1"为提交
    @Select("select reportStatus from t_member where id = #{value}")
    String selectReportStatusById(Integer id);
}
